package com.hotel.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hotel.mapper.EmployeeMapper;
import com.hotel.pojo.Employee;
import com.hotel.service.EmployeeService;
import com.hotel.utils.EncryptionUtils;
import com.hotel.utils.ResultInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 员工操作实现类
 */

@Service
public class EmployeeServiceImpl implements EmployeeService {
    //登录接口实现类
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public ResultInfo loginService(Employee loginEmployee) {
        // 加入shiro
        // 1、创建主体subject
        Subject subject = SecurityUtils.getSubject();
        // 2、判断当前主体是否登录了
        if (!subject.isAuthenticated()){
            // 未登录
            // 3、创建token
            AuthenticationToken token = new UsernamePasswordToken(loginEmployee.getEmployeName(), loginEmployee.getEmployePassword());
            // 4、执行shiro中的login方法，login方法需要一个参数，就是我们创建出来的token
            try {
                subject.login(token);
                System.out.println("登录成功");
                return new ResultInfo(200, "登录成功", loginEmployee);
            }catch (UnknownAccountException unknownAccountException){
                System.out.println("账号不存在");
                return new ResultInfo(-1,"账号不存在");
            }catch (IncorrectCredentialsException incorrectCredentialsException){
                System.out.println("密码不正确异常");
                return new ResultInfo(-2,"密码不正确异常");
            }catch (Exception e){
                System.out.println("其他异常");
                return new ResultInfo(500,"其他异常");
            }
        }
        // 登录了
        System.out.println("当前用户已经登录了");
        return new ResultInfo(-3, "当前用户已经登录了", loginEmployee);


//        Employee selectEmployee = employeeMapper.selectEmployeeByName(loginEmployee.getEmployeName());
//        if (selectEmployee.getEmployeName().equals(loginEmployee.getEmployeName()) && selectEmployee.getEmployePassword().equals(loginEmployee.getEmployePassword())) {
//            return new ResultInfo(200, "登录成功");
//        }
//        return new ResultInfo(500, "登陆失败");
    }

    @Override
    public ResultInfo selectEmployee(Integer employeeId, int page, int size) {
        //开启分页
        PageHelper.startPage(page, size);
        //获取数据
        List<Employee> employees = employeeMapper.selectAllEmployee(employeeId);
        if (employees != null) {
            //把结果封装到PageInfo中
            PageInfo<Employee> employeePageInfo = new PageInfo<>(employees);
            return new ResultInfo(200, "查询到数据", employeePageInfo);
        }
        return new ResultInfo(500, "暂无数据");
    }

    @Override
    public ResultInfo deleteEmployee(Integer employeeId) {
        Boolean deleteEmployee = employeeMapper.deleteEmployee(employeeId);
        if (deleteEmployee) {
            return new ResultInfo(200, "删除成功");
        }
        return new ResultInfo(500, "删除失败");
    }

    @Override
    public ResultInfo insertEmployee(Employee employee) {

        employee.setEmployePassword(EncryptionUtils.md5(employee.getEmployeName(), employee.getEmployePassword()));
        if (employeeMapper.insertEmployee(employee)) {
            return new ResultInfo(200, "添加成功");
        }
        return new ResultInfo(500, "添加失败");

    }

    @Override
    public ResultInfo updateEmployee(Employee employee) {
        employee.setEmployePassword(EncryptionUtils.md5(employee.getEmployeName(), employee.getEmployePassword()));
        System.out.println(employee);
        if (employeeMapper.updateEmployee(employee)) {
            return new ResultInfo(200, "修改成功");
        }
        return new ResultInfo(500, "修改失败");
    }
}
